#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QIODevice>
#include <QString>
#include <QLocale>
#include <sys/stat.h>
#include <unistd.h>
#include <QProcess>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug("have a test hello world %d",argc);
    for (int i = 0; i < argc; i++) {
     qDebug("%d %s",argc,argv[i]);
    }

    double last_time = 0,new_time = 0;
    if(argc != 2)
    {
        exit(-1);
    }
    long Total_times = 0;
    QString homeCommand = "adb shell input keyevent 3";
    QFile file(argv[1]);

    if(file.open(QIODevice::ReadOnly))
    {
        char buffer[2048];
        while (1) {
            file.seek(0);
            QProcess p;
            p.start(homeCommand);
            p.waitForStarted();
            p.waitForFinished();
            usleep(1000000);
            double last_time = 0,new_time = 0;
            qDebug("Total times %d",Total_times++);
        do{
            qint64 linelen=file.readLine(buffer,sizeof(buffer));

            if(linelen != -1)
            {
                //qDebug()<< buffer;
                QLocale c(QLocale::C);
                QString text(buffer);
                QStringList time = text.split("]");
                QString times = time[0].remove(QChar('['),Qt::CaseInsensitive);
                QString inputEventStr = time[1].replace(" "," ").trimmed();
                QStringList inputEvents = inputEventStr.split(" ");



                //qDebug() << "zyk" << inputEvents << inputEvents[2] << inputEvents[2].toLong(nullptr,16);

                //qDebug() << inputEvents;
                inputEventStr.clear();
                for (qint8 i = 0; i < inputEvents.length() ; i++) {
                    inputEventStr.append(" ");
                    inputEventStr.append(QString::number(inputEvents[i].toLong(nullptr,16),10));
                }
                //qDebug() << inputEventStr;
                inputEventStr.remove("\n");
                QString adbCommand = "adb shell sendevent /dev/input/event3" + inputEventStr;


                //qDebug() << inputEventStr;

#if 0

                for (qint8 i = 0; i < inputEvents.length() ; i++) {
                    inputEvents[i].insert(0,QString("0x"));
                }
                qDebug() << "new" << inputEvents;
                qDebug() << time.length() << time[0] << time[1];
#endif
                new_time = c.toDouble(times);

                if((last_time > 1) && ((new_time - last_time) > 0.01)){
                    usleep(1000000 * (new_time - last_time));
                    //qDebug() << "";
                }
                    QProcess p;
                    p.start(adbCommand);
                    p.waitForStarted();
                    p.waitForFinished();
                    QString valueStr = QString::fromLocal8Bit(p.readAllStandardOutput());

                //qDebug() <<fixed << qSetRealNumberPrecision(7) << new_time - last_time << time[1] << adbCommand;

                 last_time = new_time;
               // qDebug() <<   times << fixed << qSetRealNumberPrecision(7) << x ;
            }else{
                break;
            }

        }while(1);

        }

        exit(0);

    }
    return a.exec();
}
